package com.chuan.thinkingjava.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPool {
	
	public static void main(String[] args) {
		ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 8, 2, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024));
		List<Task> list = new ArrayList<>();
		list.add(new Task("sb"));
		list.add(new Task("hah"));
		list.add(new Task("rr"));
		list.add(new Task("sb"));
		list.add(new Task("hah"));
		list.add(new Task("rr"));
		list.add(new Task("sb"));
		list.add(new Task("hah"));
		list.add(new Task("rr"));
		list.add(new Task("sb"));
		list.add(new Task("hah"));
		list.add(new Task("rr"));
		list.add(new Task("sb"));
		list.add(new Task("hah"));
		list.add(new Task("rr"));
		list.add(new Task("sb"));
		list.add(new Task("hah"));
		list.add(new Task("rr"));
		list.add(new Task("sb"));
		list.add(new Task("hah"));
		list.add(new Task("rr"));
		list.add(new Task("sb"));
		list.add(new Task("hah"));
		list.add(new Task("rr"));
		list.add(new Task("sb"));
		list.add(new Task("hah"));
		list.add(new Task("rr"));
		for (Task task : list) {
			threadPool.execute(task);
		}
	}
}

class Task implements Runnable {

	private String str;
	
	public Task(String str) {
		this.str = str;
	}
	
	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName() + "执行了：" + str);
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Thread.yield();
		System.out.println(Thread.currentThread().getName() + "让给别人执行完了");
	}
}